package com.itheima.功能流.序列化流;

import java.io.*;
import java.util.ArrayList;

public class 序列化流 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        method1();
        ArrayList<User> users;
        //创建对象输入流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.txt"));

        //读取集合
        users = (ArrayList<User>) ois.readObject();

        System.out.println(users);


        //关流,释放资源
        ois.close();
    }

    private static void method1() throws IOException {
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("zhangsan","123456"));
        users.add(new User("lisi","qwer"));
        users.add(new User("wangwu","wedc"));
        users.add(new User("zhaoliu","admin"));
        users.add(new User("qianqi","root"));
        users.add(new User("zhaoba","tester"));

        //创建对象输出流
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.txt"));

        //写入集合对象
        oos.writeObject(users);

        //关流,释放资源
        oos.close();
    }
}
